package com.animatron.sample.client;

import com.animatron.sample.client.tree.TreeComponent;
import com.animatron.sample.client.tree.json.JsonLoader;
import com.google.gwt.canvas.client.Canvas;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.dom.client.TextAreaElement;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class sample implements EntryPoint
{
	private TreeComponent tree;

	public void onModuleLoad()
	{
		Button button = new Button("Update");
		button.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event)
			{
				updateFromJson();
			}
		});
		RootPanel.get("treeContainer").add(button);

		if (Canvas.isSupported())
		{
			tree = new TreeComponent();
			RootPanel.get("treeContainer").add(tree);
			updateFromJson();
		}
		else
			RootPanel.get("treeContainer").add(new Label("Canvas not supported on this browser"));
	}

	void updateFromJson()
	{
		Element elementById = DOM.getElementById("jsondata");
		TextAreaElement textArea = TextAreaElement.as(elementById);
		if (textArea == null)
			Window.alert("TextArea with id \"jsondata\" not found");
		else
		{
			new JsonLoader(textArea.getValue(), tree.getRoot()).load();
			tree.repaint();
		}
	}
}
